<?php

/**
 * bugtracker actions.
 *
 * @package    vo-ser
 * @subpackage bugtracker
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class bugtrackerActions extends sfActions
{

    /**
     * @see sfAction::preExecute()
     */
    public function preExecute()
    {
		$pr_slug = $this->getRequest()->getParameter('pr_slug');
		$this->project = Doctrine::getTable('Project')->findOneByName(SER::deSlugify($pr_slug));
    }

    /**
     * Executes index action
     *
     * @param sfRequest $r A request object
     */
    public function executeIndex(sfWebRequest $r)
    {
        $r->setParameter('id', 1);
        $this->forward('bugtracker', 'showReport');
    }

    public function executeShow(sfWebRequest $r)
    {
        $bugID = $r->getParameter('id');
        $this->bug = Doctrine::getTable('Bug')->getByID($bugID);
		
		$bugComment = new BugComment();
		$bugComment->setBug($this->bug);
		$bugComment->setUser($this->getUser()->getUserObject());
		$bugComment->setActions('No actions');
		
        $this->form = new NewBugCommentForm($bugComment);

        if ($r->isMethod('put'))
        {
            $this->form->bind($r->getParameter($this->form->getName()));
			
			if ($this->form->isValid())
			{
				$this->form->save();
				$this->redirect($this->bug->getUrl());
			}
			
        }
    }

    public function executeNew(sfWebRequest $r)
    {
		$newBug = new Bug();
		$newBug->setProject($this->project);
		$newBug->setCreator($this->getUser()->getUserObject());
		
		$this->form = new NewBugForm($newBug);
		
		if ($r->isMethod('put'))
        {
            if (($bug = $this->processBugForm($r, $this->form)) !== false)
				$this->redirect($bug->getUrl());
        }
    }

    public function executeEdit(sfWebRequest $r)
    {
        $bugID = $r->getParameter('id');
		if (!$bugID)
			$this->redirect('@bug_index?pr_slug='.$this->project->getNameSlug());
			
        $bug = Doctrine::getTable('Bug')->getByID($bugID);
		
		if (!$bug)
			$this->redirect('@bug_index?pr_slug='.$this->project->getNameSlug());
		
		$this->form = new NewBugForm($bug);
		
		if ($r->isMethod('put'))
        {
			$old = $bug->getData();
			
			if (($newBug = $this->processBugForm($r, $this->form)) === false)
				return;
			
			$taint = array();
			foreach(array_keys($old) as $key)
			{
				if ($key == 'created_at')
					continue;
					
				if ($key == 'updated_at')
					continue;
				
				if ($old[$key] != $this->form->getValue($key))
					$taint[$key] = $this->form->getValue($key);
			}
			
			$newBug = $this->form->getObject();
			
			$action = array();
			foreach($taint as $key => $val)
			{
				switch ($key) {
					case 'owner_id':
						$v = Doctrine::getTable('User')->find($val);
						if ($v == null) break;
						$action[] = 'Owner = '.$v;
					break;
					
					case 'type_id':
						$v = Doctrine::getTable('BugType')->find($val);
						$action[] = 'Type = '.$v;
					break;
					
					case 'status_id':
						$v = Doctrine::getTable('BugStatus')->find($val);
						$action[] = 'Status = '.$v;
					break;
					
					case 'priority_id':
						$v = Doctrine::getTable('BugPriority')->find($val);
						$action[] = 'Priority = '.$v;
					break;
					
					case 'milestone_id':
						$v = Doctrine::getTable('BugMilestone')->find($val);
						if ($v == null) break;
						$action[] = 'Milestone = '.$v;
					break;
					
					case 'due_date':
						if ($val == '') break;
						$action[] = 'Due date = '.$val;
					break;
					
					case 'description':
						$action[] = 'Description = '.substr($val, 0, 15).'...';
					break;
					
					case 'summary':
						$action[] = 'Summary = '.substr($val, 0, 15).'...';
					break;
					
					default:
						$action[] = "$key = $val";
					break;
				}
			}
			
			$bc = new BugComment();
			$bc->setUser($this->getUser()->getUserObject());
			$bc->setBug($bug);
			$bc->setContent($this->form->getValue('comment'));
			if (count($action) == 0)
				$bc->setActions('No actions...');
			else
				$bc->setActions(implode(', ', $action));
			$bc->save();
			
			$this->redirect($bug->getUrl());
        }
    }

    public function executeShowReport(sfWebRequest $r)
    {
		$reportID = $r->getParameter('id');
		
		$this->bugReport = Doctrine::getTable('BugReport')->find($reportID);
		$this->bugs = $this->bugReport->getBugs();
		$this->columns = $this->bugReport->getColumns();
    }
	
	public function executeNewReport(sfWebRequest $r)
	{
		$this->url = $r->getUri();
		$this->form = new NewBugReportForm();
		
		if ($r->isMethod('put'))
		{
			$this->form->bind($r->getParameter($this->form->getName()));
			
			if ($this->form->isValid())
			{
				$bugReport = new BugReport();
				$bugReport->setName($this->form->getValue('name'));
				if (!$this->form->getValue('global'))
					$bugReport->setOwner($this->getUser()->getUserObject());
				$bugReport->setSortBy($this->form->getValue('sort_by'));
				$bugReport->setSortDirection($this->form->getValue('sort_direction'));
				
				// conditions
				$conditions = array();
				foreach($this->form->getValue('conds') as $key => $v)
				{
					$op = $v['op'];
					$val = $v['val'];
					
					if ($val == '')
						continue;
						
					$conditions[] = "$key-$op-$val";
				}
				$bugReport->setConditions(implode(',', $conditions));
				
				// fields
				$bugReport->setFields(implode(',', $this->form->getValue('fields')));
				
				$bugReport->save();
				
				$this->redirect($bugReport->getUrl());
			}
		}
	}
	
	public function executeDeleteReport(sfWebRequest $r)
	{
		$reportID = $r->getParameter('id');

        $bugReport = Doctrine::getTable('BugReport')->find($reportID);
		if (!$bugReport)
			$this->redirect('@bug_index?pr_slug='.$this->project->getNameSlug());
			
        $bugReport->delete();
		
		$this->redirect('@bug_index?pr_slug='.$this->project->getNameSlug());
	}
	
	private function processBugForm(sfWebRequest $request, sfForm $form)
	{
		$form->bind($request->getParameter($form->getName()));
		
		if ($form->isValid())
		{
			return $form->save();
		}
		else
		{
			return false;
		}
	}

}
